<template>
	<view class="cart-container" v-if="cart.length !== 0">
		<!-- 收货地址 -->
		<my-address></my-address>
		<!-- 标题区域 -->
		<view class="cart-title">
			<uni-icons type="shop" size="18"></uni-icons>
			<text class="cart-title-name">购物车</text>
		</view>
		<!-- 商品区域 -->
		<view class="goods-list">
			<uni-swipe-action>
				<block v-for="(goods,i) in cart" :key="i">
					<uni-swipe-action-item :right-options="options" @click="deleteCartItem(goods)">
						<my-goods :goods="goods" :showRadio="true" @change-radio="changeRadio" showNum="true" @change-num="changeNum"></my-goods>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
		</view>
		<!-- 结算区域 -->
		<my-settle></my-settle>
	</view>
	<!-- 购物车为空时 -->
	<view class="empty-cart" v-else>
		<image src="/static/cart_empty@2x.png" class="empty-img"></image>
		<text class="tipText">空空如也~</text>
	</view>
</template>

<script>
	import {mapGetters,mapState,mapMutations} from 'vuex'
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		computed:{
			...mapState('my_cart',['cart']),
			...mapGetters('my_cart',['total'])
		},
		watch:{//设置监听属性，当购物车总数量发生改变，修改tabbar角标值
			total(){
				this.setBadge()
			}
		},
		mixins:[badgeMix],
		data() {
			return {
				options:[{
					text:'删除',
					style:{
						backgroundColor:'#c00000'
					}
				}]
			};
		},
		methods:{
			...mapMutations('my_cart',['updateCart','updateNum','deleteGoodsById']),
			changeRadio(e){
				this.updateCart(e)
			},
			changeNum(e){
				this.updateNum(e)
			},
			deleteCartItem(goods){
				this.deleteGoodsById(goods.goods_id)
			}
		}
	}
</script>

<style lang="scss">
.cart-container{
	padding-bottom: 50px;
	.cart-title{
		display: flex;
		align-items: center;
		height: 40px;
		padding-left: 5px;
		border: 1px solid #efefef;
		.cart-title-name{
			font-size: 14px;
			color: #grey;
			margin-left: 10px;
		}
	}
}
.empty-cart{
	display: flex;
	flex-direction: column;
	padding-top: 300rpx;
	align-items: center;
	.empty-img{
		width: 180rpx;
		height: 180rpx;
	}
	.tipText{
		font-size: 13px;
		color: gray;
		margin-top: 10px;
	}
}
</style>
